package github.sf.fw.tools.db.impl.neo4j;


import github.sf.fw.tools.db.AbstractTableDataWriter;
import github.sf.fw.tools.db.DSConnector;
import github.sf.fw.tools.db.DSProperty;
import github.sf.fw.tools.db.connector.Neo4jConnector;
import github.sf.fw.tools.db.model.ResultData;
import github.sf.fw.tools.db.model.ResultRow;
import github.sf.fw.tools.db.model.TableInfo;

import java.util.List;

public class Neo4jTableDataWriter extends AbstractTableDataWriter {

    public Neo4jTableDataWriter(DSProperty dsProperty) {
        super(dsProperty);
    }


    @Override
    public int writer(TableInfo tableInfo, List<ResultRow> resultRowList) {
        writer(new Neo4jConnector(dsProperty), tableInfo, resultRowList);
        return 0;
    }

    /**
     * 只需要重写这个方法即可 从mysql导入到neo4j当中
     *
     * @param dsConnector
     * @param tableInfo
     * @param resultRowList
     */
    @Override
    public void writer(DSConnector dsConnector, TableInfo tableInfo, List<ResultRow> resultRowList) {
        Neo4jConnector neo4JConnector = (Neo4jConnector) dsConnector;
        //处理插入映射
//        MappingJsonUtils.totranfer(tableInfo.getTableExtraInfo().getGraphInfo().getJson(), resultRowList);
        //处理点
        if (tableInfo.getTableExtraInfo().getGraphInfo().getGrpthType().equalsIgnoreCase("tag")) {
            String result = "create (p:%s{%s})";
            String releation = "";
            String targetTableName = tableInfo.getTableExtraInfo().getTargetTableName();
            int len = resultRowList.size();
            int offset = 0;
            int index = 0;
            //对于导入的数据量的循环
            while (index < len) {
                StringBuilder sql = new StringBuilder();
                for (int i = 0; i < 10000; i++) {
                    if (index == len) {
                        break;
                    }
                    ResultRow resultRow = resultRowList.get(index);
                    for (int i1 = 0; i1 < resultRow.size(); i1++) {
                        ResultData resultData = resultRow.get(i1);
                        sql.append(resultData.getColumnName()).append(":").append("'").append(resultData.getColumnValue()).append("'").append(",");
                    }
                    sql.deleteCharAt(sql.length() - 1);
                    releation = sql.toString();
                    sql.setLength(0);
                    String format = String.format(result, targetTableName, releation);
                    //插入数据
                    neo4JConnector.dowrite(format);
                    index++;
                }
                offset++;
            }

        }
        //处理边
        else if (tableInfo.getTableExtraInfo().getGraphInfo().getGrpthType().equalsIgnoreCase("edge")) {

            String result = "match (a:%s{id:'%s'}),(b:%s{id:'%s'}) create(a)-[:%s%s] ->(b)";
            int len = resultRowList.size();
            int index = 0;
            int offset = 0;
            String edgeFrom = tableInfo.getTableExtraInfo().getGraphInfo().getEdgeFrom();
            String edgeTo = tableInfo.getTableExtraInfo().getGraphInfo().getEdgeTo();
            String edgeFromId = "";
            String edgeToId = "";
            String relationPor = "";
            String targetTableName = tableInfo.getTableExtraInfo().getTargetTableName();
            //遍历数据 就是每一行的数据
            while (index < len) {
                StringBuilder stringBuilder = new StringBuilder();
                stringBuilder.append("{");
                for (int i = 0; i < 10000; i++) {
                    if (index == len) {
                        break;
                    }
                    ResultRow resultRow = resultRowList.get(index);
                    for (int i1 = 0; i1 < resultRow.size(); i1++) {
                        ResultData resultData = resultRow.get(i1);
                        if (resultData.getColumnName().equalsIgnoreCase("from")) {
                            edgeFromId = resultData.getColumnValue().toString();
                        } else if (resultData.getColumnName().equalsIgnoreCase("to")) {
                            edgeToId = resultData.getColumnValue().toString();
                        } else {
                            stringBuilder.append(resultData.getColumnName()).append(":").append("'").append(resultData.getColumnValue()).append("'").append(",");
                        }
                    }
                    stringBuilder.deleteCharAt(stringBuilder.length() - 1);
                    relationPor = stringBuilder.toString();
                    stringBuilder.setLength(0);
                    String format = String.format(result, edgeFrom, edgeFromId, edgeTo, edgeToId, targetTableName, relationPor);
                    //这里插入数据
                    neo4JConnector.dowrite(format);
                    index++;
                }
                offset++;
            }
        }
    }


    @Override
    public void deleteData(TableInfo tableInfo) {
    }

    @Override
    public void deleteTable(TableInfo tableInfo) {
    }

    @Override
    public void doUpdate(String script, Object[] args) {
    }
}
